package com.leo.foodmaster.admin.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.leo.foodmaster.commons.constants.Constants;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.foodmaster.admin.system.entity.SysLoginLogEntity;
import com.leo.foodmaster.admin.system.mapper.SysLoginLogMapper;
import com.leo.foodmaster.admin.system.service.ISysLoginLogService;
import lombok.AllArgsConstructor;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 系统访问记录
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2021-12-22
 */
@Service
@AllArgsConstructor
public class ISysLoginLogServiceImpl extends ServiceImpl<SysLoginLogMapper, SysLoginLogEntity> implements ISysLoginLogService {

	private final SysLoginLogMapper sysLoginLogMapper;

	/**
	 * 异步保存登录日志
	 * @param ip ip
	 * @param username 用户名称
	 * @param status 状态
	 * @param message 消息
	 */
	@Async
	public void asyncSaveLoginLog(String ip, String username, String status, String message) {
		// 封装对象
		SysLoginLogEntity loginLogEntity = new SysLoginLogEntity();
		loginLogEntity.setUserName(username);
		loginLogEntity.setIpaddr(ip);
		loginLogEntity.setMsg(message);
		loginLogEntity.setLoginTime(LocalDateTime.now());
		// 日志状态
		if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status)) {
			loginLogEntity.setStatus("0");
		}
		else if (Constants.LOGIN_FAIL.equals(status)) {
			loginLogEntity.setStatus("1");
		}
		save(loginLogEntity);
	}

	@Override
	public IPage<SysLoginLogEntity> pageList(Page<SysLoginLogEntity> page, String userName, String ipAddr, String status, String beginTime, String endTime) {
		LambdaQueryWrapper<SysLoginLogEntity> eq = Wrappers.lambdaQuery(SysLoginLogEntity.class)
				.like(StrUtil.isNotBlank(userName), SysLoginLogEntity::getUserName, userName)
				.like(StrUtil.isNotBlank(ipAddr), SysLoginLogEntity::getIpaddr, ipAddr)
				.like(StrUtil.isNotBlank(status), SysLoginLogEntity::getStatus, status)
				.ge(StrUtil.isNotBlank(beginTime), SysLoginLogEntity::getLoginTime, beginTime)
				.le(StrUtil.isNotBlank(endTime), SysLoginLogEntity::getLoginTime, endTime);
		return sysLoginLogMapper.selectPage(new Page<>(page.getCurrent(),page.getSize()), eq);
	}

	@Override
	public List<SysLoginLogEntity> getList(String userName, String ipaddr, String status, String beginTime, String endTime) {
		LambdaQueryWrapper<SysLoginLogEntity> eq = Wrappers.lambdaQuery(SysLoginLogEntity.class)
				.like(StrUtil.isNotBlank(userName), SysLoginLogEntity::getUserName, userName)
				.like(StrUtil.isNotBlank(ipaddr), SysLoginLogEntity::getIpaddr, ipaddr)
				.like(StrUtil.isNotBlank(status), SysLoginLogEntity::getStatus, status)
				.ge(StrUtil.isNotBlank(beginTime), SysLoginLogEntity::getLoginTime, beginTime)
				.le(StrUtil.isNotBlank(endTime), SysLoginLogEntity::getLoginTime, endTime);
		return sysLoginLogMapper.selectList(eq);
	}
}
